<template>
  <div class="login-back">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="ruleForm">
      <el-form-item label="工号" prop="userId" class="text-white">
        <el-input type="text" v-model="ruleForm.userId" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="pass" class="text-white">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item style="width: 100%">
        <el-button type="primary" style="width: 100%" @click="submitForm('ruleForm')">登录</el-button>
        <!--<el-button @click="resetForm('ruleForm')">重置</el-button>-->
      </el-form-item>
      <el-form-item>
        <el-link type="primary" href="/users/add">新用户注册</el-link>
      </el-form-item>
    </el-form>
  </div>

</template>


<script>
import axios from "../../plugins/axiosConfig";
import {loginUrl} from "../../utils/api";
import Cookies from 'js-cookie'
// import qs from "qs"

export default {
  data() {
    var checkUserId = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入工号'));
      } else if (value.length !== 6) {
        return callback(new Error('工号长度为6位'));
      }
      callback();
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      }
      callback();
      // else {
      //   if (this.ruleForm.checkPass !== '') {
      //     this.$refs.ruleForm.validateField('checkPass');
      //   }
      //   callback();
      // }
    };

    return {
      ruleForm: {
        userId: '',
        pass: ''
      },
      rules: {
        userId: [
          {validator: checkUserId, trigger: 'blur'}
        ],
        pass: [
          {validator: validatePass, trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');


          axios.post(loginUrl + '?userId=' + this.$data.ruleForm.userId + '&password=' + this.$data.ruleForm.pass
          ).then((res) => {
            console.log("--------res", res)
            if (res.status === 200) {
              if (res.data) {
                // alert("登陆成功,token: " + res.data.data);
                console.log("登陆成功,token: " + res.data.data);
                this.$router.push('/my');
                Cookies.set('user', this.$data.ruleForm.userId, 7);
                Cookies.set('token', res.data.data, 1);
              }
              return res.data
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  name: "Login"
}
</script>

<style>
.login-back {
  background-image: url("http://static.yudao.iocoder.cn/login-background.jpg");
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100%;
}


.ruleForm {
  width: 80%;
  margin-right: 10%;
}

.text-white .el-form-item__label {
  color: white;
}

</style>